#!/usr/bin/python
import libxml2, libxslt
import os, time, sys
import xml.etree.ElementTree as ET

class compoundXML:
    def __init__(self):
        self._result = None
        self._xsl = None
        self._xml = None

       
    def do(self, xml_file_name, xsl_file_name):       
        self._xml = libxml2.parseFile(xml_file_name)
        if self._xml == None:
            return 0
        styledoc = libxml2.parseFile(xsl_file_name)
        if styledoc == None:
            return 0
        self._xsl = libxslt.parseStylesheetDoc(styledoc)
        if self._xsl == None:
            return 0
        self._result = self._xsl.applyStylesheet(self._xml, None)               

    def get_xml_doc(self):
        return self._result           

    
        
    def get_translated(self):
        return self._result.serialize('UTF-8')       

    def save_translated(self, file_name):
        self._xsl.saveResultToFilename(file_name, self._result, 0)

    def release(self):
        '''
        this function must be called in the end.
        '''
        self._xsl.freeStylesheet()
        self._xml.freeDoc()
        self._result.freeDoc()
        self._xsl = None
        self._xml = None
        self._result = None
        
    def get_cunit_result(self):
        return self._xml.xpathEval('//CUNIT_RUN_SUMMARY_RECORD/TOTAL')[1].content, self._xml.xpathEval('//CUNIT_RUN_SUMMARY_RECORD/SUCCEEDED')[1].content, self._xml.xpathEval('//CUNIT_RUN_SUMMARY_RECORD/FAILED')[1].content

if __name__ == '__main__':
    html = open('index.html', 'w')
    html.write("""
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html lang="en">

    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>CUnit report</title>
      <link rel="stylesheet" type="text/css" href="gcov.css">
    </head>

    <body>

      <table width="100%" border=0 cellspacing=0 cellpadding=0>
        <tr><td class="title">CUnit report</td></tr>
        <tr><td class="ruler"><img src="glass.png" width=3 height=3 alt=""></td></tr>
      </table>

      <center>
      <table width="80%" cellpadding=1 cellspacing=1 border=0>

        <tr>
          <td width="20%"></td>
          <td width="20%"></td>
          <td width="20%"></td>
          <td width="20%"></td>
        </tr>
        <tr>
          <td class="tableHead">Name <span class="tableHeadSort"><img src="glass.png" width=10 height=14 alt="Sort by name" title="Sort by name" border=0></span></td>
          <td class="tableHead">Total <span class="tableHeadSort"><img src="glass.png" width=10 height=14 alt="Sort by function Total" title="Sort by function Total" border=0></span></td>
          <td class="tableHead">Success <span class="tableHeadSort"><img src="glass.png" width=10 height=14 alt="Sort by branch Success" title="Sort by branch Success" border=0></span></td>
          <td class="tableHead">Fail <span class="tableHeadSort"><img src="glass.png" width=10 height=14 alt="Sort by branch Fail" title="Sort by branch Fail" border=0></span></td>
        </tr>   
    """)
    
    ut_result = 0
    ut_report='''
    <tr>
      <td class="coverFile"><a href="%(name)s/ut-report.html">%(name)s</a></td>
      <td class="coverPerHi">%(total)s</td>
      <td class="coverNumHi">%(success)s</td>
      <td class="coverPerLo">%(fail)s</td>
    </tr>
    '''
    for fpath,dirs,fs in os.walk('./'):
        for dir in dirs:
            os.chdir(dir)
            test = compoundXML()
            test.do('Automated-Results.xml', 'CUnit-Run.xsl')
            #print test.get_translated()
            test.save_translated('ut-report.html')
            (total,success,fail) = test.get_cunit_result()
            #print "xdf======="+success
            data = {'name':dir, 'total':total, 'success':success, 'fail':fail}
            if int(fail) > 0:
                ut_result = 1
            test.release()
            os.chdir('../')
            html.write(ut_report % data)
        break
     
    html.write('''
    </table>
      </center>
      <br>

      <table width="100%" border=0 cellspacing=0 cellpadding=0>
        <tr><td class="ruler"><img src="glass.png" width=3 height=3 alt=""></td></tr>
    ''')
    end_html='''
        <tr><td class="versionInfo">Generated by: xudengfeng 00173555  at  %(create_time)s</a></td></tr>
    '''
    ISOTIMEFORMAT='%Y-%m-%d %X'
    time.strftime(ISOTIMEFORMAT, time.localtime())
    data1 = {'create_time':time.strftime(ISOTIMEFORMAT, time.localtime())}
    html.write(end_html % data1)
    html.write('''
      </table>
      <br>

    </body>
    </html>
    ''')
    
    sys.exit(ut_result)
